/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fauxman.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.RectangularShape;
import java.util.Collection;

import javax.swing.JApplet;

import fauxman.StaticElements.GhostHouseArea;
import fauxman.StaticElements.PlayableArea;
import fauxman.StaticElements.Wormhole;
import fauxman.controller.FauxmanController;
import fauxman.interfaces.DynamicElement;
import fauxman.model.WorldModel;

/**
 *
 * @author K7
 */
public class FauxManView extends JApplet{
	
	WorldModel model;
	FauxmanController fc;
	Graphics g;
	private Image offScreenImage;
	private Graphics offScreenGraphics;

    /**
     * @param args the command line arguments
     */
	public void init(){
		model = new WorldModel();
		fc = new FauxmanController(model);
		addKeyListener(fc);
		setFocusable(true);
		this.resize(673, 745);	// Add one to each dimension so window does not clip the game screen
		offScreenImage = createImage(getWidth(), getHeight());
		offScreenGraphics = offScreenImage.getGraphics();
	}
	
	
    
    public void paint(Graphics g) {
    	offScreenGraphics.clearRect(0, 0, getWidth(), getHeight());
    	this.g = offScreenGraphics;
    	model.move();
    	redraw();
    	g.drawImage(offScreenImage, 0, 0, this);
    	try {
			Thread.currentThread().sleep(19);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	repaint();
    }
 

	private void redraw() {
    	makeRectangle(Color.BLACK, 0,0,672,744);	// original pacman resolution: 224x288
    	makeBoundaries();
    	makeStaticElements();
    	makeDynamicElements();

    	model.drawGrid(g);
	}
	
	private void makeStaticElements() {
		Collection<PlayableArea> sE = model.getPlayableArea();
		for(PlayableArea x:sE){
			x.draw(g);
		}
		Collection<GhostHouseArea> gH = model.getGhostHouseArea();
		for(GhostHouseArea x:gH){
			x.draw(g);
		}
		Collection<Wormhole> wh = model.getWormholes();
		for(Wormhole x:wh){
			x.draw(g);
		}
		Collection<PlayableArea> cA = model.getCorneringArea();
		for(PlayableArea x: cA){
			x.draw(g);
		}
	}
	
	private void makeDynamicElements() {
		g.setColor(Color.WHITE);
    	Collection<DynamicElement> dE = model.getDynamicElements();
		for(DynamicElement x:dE){
			x.draw(g);
		}
	}

	private void makeBoundaries() {
    	RectangularShape[] boundary = model.getBoundary();
		for(RectangularShape current:boundary){
	    	makeRectangle(Color.BLUE, current.getX(),current.getY(),current.getWidth(),current.getHeight());
		}
		for(RectangularShape current:boundary){
	    	makeRectangle(Color.BLACK,  current.getX() + model.getOutlineWidth() ,current.getY() + model.getOutlineWidth() ,current.getWidth() - (2 * model.getOutlineWidth()), current.getHeight() - (2 *  model.getOutlineWidth()));
		}
	}
    

	public void makeRectangle(Color col, double x, double y, double width, double height){
    	g.setColor(col);
    	g.fillRect((int)x,(int)y, (int)width, (int)height); 
    }
	
}
